package cn.befory.support;

import java.util.concurrent.atomic.AtomicLong;

public class AtomicShortUrlGenerator implements ShortUrlGenerator{
    /**
     * id生成
     */
    private AtomicLong atomicLong;


    public AtomicShortUrlGenerator(Long start) {
        this.atomicLong = new AtomicLong(start);
    }

    @Override
    public String generate() {
        StringBuilder sb = new StringBuilder();
        long id = atomicLong.getAndIncrement();
        do {
            int i = (int) (id % BASE.length());
            sb.append(BASE.charAt(i));
            id /= BASE.length();
        } while (id > 0);
        return sb.toString();
    }
}
